package com.zisuyi.uilibrary.module.dialog.redeem

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import com.weilele.mvvm.base.MvvmKeyboardDialog
import com.weilele.mvvm.base.helper.LiveDataWrap
import com.weilele.mvvm.base.helper.StatusValue
import com.weilele.mvvm.base.helper.annotation.MvvmViewModel
import com.weilele.mvvm.base.helper.appCompatActivity
import com.weilele.mvvm.base.helper.wrapObserver
import com.weilele.mvvm.utils.activity.*
import com.weilele.mvvm.utils.fragment.arguments
import com.weilele.mvvm.utils.safeGet
import com.zisuyi.game.sdk.net.bean.RedeemBean
import com.zisuyi.game.sdk.net.bean.UserInfoBean
import com.zisuyi.uilibrary.databinding.DialogRedeemEmailBinding
import com.zisuyi.uilibrary.module.activty.redeem.RedeemViewModel
import com.zisuyi.uilibrary.module.dialog.SimpleTextDialog.Companion.simpleTextDialog


class RedeemEmailDialog : MvvmKeyboardDialog() {
    private lateinit var binding: DialogRedeemEmailBinding

    companion object {
        fun show(appCompatActivity: AppCompatActivity?, itemData: UserInfoBean.RedeemDTO?) {
            appCompatActivity ?: return
            itemData ?: return
            if (itemData.remainingCount <= 0) {
                //不能兑换
                appCompatActivity.simpleTextDialog {
                    this.setTitle("Tips")
                    this.buttonText = "Cancel"
                    this.content =
                        "Since you have redeemed this amount, it cannot be redeemed again for the time being."
                    this.buttonNext.onClick {
                        this.dismiss()
                    }
                }
            } else {
                showRedeem(appCompatActivity, itemData)
            }
        }

        private fun showRedeem(
            appCompatActivity: AppCompatActivity?,
            itemData: UserInfoBean.RedeemDTO?
        ) {
            RedeemEmailDialog().apply {
                arguments {
                    putSerializable("RedeemDTO", itemData)
                }
            }.show(appCompatActivity)
        }
    }

    private val redeemDTO by lazy {
        arguments?.getSerializable("RedeemDTO").safeGet<UserInfoBean.RedeemDTO>()
    }

    override fun getFocusViewView(): EditText? {
//        return etEmail
        return null
    }

    override fun getLayoutResOrView(inflater: LayoutInflater, parent: ViewGroup?): Any? {
        binding = DialogRedeemEmailBinding.inflate(inflater, parent, false)
        return binding.root
    }

    @MvvmViewModel
    private lateinit var redeemViewModel: RedeemViewModel

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        binding.ivDialogClose.onClick {
            dismiss()
        }
        binding.btRedeem.onClick {
            redeemViewModel.redeem(binding.etEmail, binding.etEmailAgain, redeemDTO)
        }
        redeemDTO?.let {
            binding.tvMoney.textFrom("${it.symbol}${it.amount}")
            binding.tvDiamond.textFrom("${it.diamond}")
        }
    }

    override fun getObserverLiveData(): List<LiveDataWrap>? {
        return listOf(redeemViewModel.redeemData wrapObserver ::onRedeem)
    }

    private fun onRedeem(value: StatusValue<RedeemBean>?) {
        when (value) {
            is StatusValue.Running -> {
                hiddenKeyboard()
                isCancelable = false
                binding.ivDialogClose.isEnabled = false
                binding.pbRedeem.visible()
                binding.btRedeem.invisible()
            }
            is StatusValue.Error -> {
                isCancelable = true
                binding.ivDialogClose.isEnabled = true
                binding.pbRedeem.invisible()
                binding.btRedeem.visible()
            }
            is StatusValue.Success -> {
                isCancelable = true
                binding.ivDialogClose.isEnabled = true
                binding.pbRedeem.invisible()
                binding.btRedeem.visible()
                "Redeem Success,Please wait send".toast()
                dismiss()
            }
        }
    }

    override fun onRootViewLayoutParams(lp: FrameLayout.LayoutParams) {
        super.onRootViewLayoutParams(lp)
        lp.marginStart = dip(32)
        lp.marginEnd = lp.marginStart
        lp.gravity = Gravity.CENTER
    }

    override fun onImeStatusChange(height: Int, imeHeight: Int): Boolean {
        binding.nsRedeem.scrollTo(0, binding.nsRedeem.getChildAt(0).height)
        return super.onImeStatusChange(height, imeHeight)
    }
}